package org.example.server.goods.controller.goods;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.api.goods.goods.controller.GoodsControllerFeign;
import org.example.api.goods.goods.request.AddGoodsRequest;
import org.example.api.goods.goods.request.DeleteGoodsRequest;
import org.example.api.goods.goods.request.GoodsSearchRequest;
import org.example.api.goods.goods.response.GoodsFatDetailResponse;
import org.example.api.goods.goods.response.GoodsListItemResponse;
import org.example.frw.api.base.intf.ID;
import org.example.server.goods.mapstruct.goods.GoodsConverter;
import org.example.server.goods.service.goods.GoodsService;
import org.example.server.goods.service.goods.bo.GoodsListItemBo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
public class GoodsController implements GoodsControllerFeign {
  private final GoodsService goodsService;
  @Resource
  private GoodsConverter goodsConverter;

  public GoodsController(GoodsService goodsService) {
    this.goodsService = goodsService;
  }

  /**
   * 搜索商品
   * @param request request
   * @return list
   */
  @PostMapping("/goods-manage/goods")
  public List<GoodsListItemResponse> goods(@RequestBody GoodsSearchRequest request){
    List<GoodsListItemBo> goods = goodsService.goods(request);
    return goodsConverter.toGoodsListItemResponse(goods);
  }


  /**商品数量
   * @param request request
   * @return count
   */
  @PostMapping("/goods-manage/goods/count")
  public Long goodsCount(@RequestBody GoodsSearchRequest request){
    return goodsService.goodsCount(request);
  }

  /**商品详情
   * @param id id
   * @return detail
   */
  @Override
  public GoodsFatDetailResponse fatDetail(Long id) {
    return goodsConverter.toGoodsFatDetailResponse(goodsService.fatDetail(id));
  }

  /**添加商品
   * @param request request
   * @return id
   */
  @PostMapping("/goods-manage/goods/add")
  public ID addGoods(@RequestBody AddGoodsRequest request){
    return goodsService.addGoods(request);
  }

  /**
   * 删除商品
   * @param request request
   */
  @PostMapping("/goods-manage/goods/delete")
  public void deleteGoods(@RequestBody DeleteGoodsRequest request){
    goodsService.deleteGoods(request);
  }
}
